package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.Competitor;
import com.crm.model.entity.customerManager.DemandResearch;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.CompetitorDbService;
import com.crm.service.opportunityManager.DemandResearchDbService;
import com.crm.system.service.opportunityManager.CompetitorService;
import com.crm.system.service.opportunityManager.DemandResearchService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 竞品分析
 */
@Service
public class CompetitorServiceImpl implements CompetitorService {

    @Autowired
    private CompetitorDbService competitorDbService;

    /**
     * 商机-商机详情-竞品分析
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public DhPageInfo<Competitor> competitorList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String opportunityId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<Competitor> list = competitorDbService.competitorList(opportunityId,"create_tm desc");
        PageInfo<Competitor> pageInfo = new PageInfo<Competitor>(list);
        return new DhPageInfo<Competitor>(pageInfo, list);
    }

    /**
     * 商机-商机详情-修改竞品分析
     * @param competitorId 竞品分析id
     * @return
     */
    @Override
    public Competitor getCompetitorById(String competitorId) {
        return competitorDbService.getCompetitorById(competitorId);
    }

    /**
     * 商机详情-修改竞品分析
     * @param competitor 竞品分析实体
     * @return
     */
    @Override
    public Boolean updateCompetitor(Competitor competitor) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        competitor.setUpdatePeopleId(userAccount.getAccountId());
        competitor.setUpdateTm(new Date());
        Boolean result = competitorDbService.updateCompetitor(competitor);
        return result;
    }

    /**
     * 商机详情-删除竞品分析
     * @param competitor 竞品分析实体
     * @return
     */
    @Override
    public Boolean deleteCompetitor(Competitor competitor) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        competitor.setUpdatePeopleId(userAccount.getAccountId());
        competitor.setUpdateTm(new Date());
        competitor.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return competitorDbService.updateCompetitor(competitor);
    }
}
